package com.tkjt.pms.modules.repos.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.calc.CalcDataUpdater;
import com.tkjt.pms.calc.CalcTraceUpdater;
import com.tkjt.pms.calc.common.DepartOwner;
import com.tkjt.pms.calc.common.IndexCalcConsts;
import com.tkjt.pms.calc.common.IndexCalcParallel;
import com.tkjt.pms.calc.formula.FormulaEngine;
import com.tkjt.pms.calc.formula.common.EngineResult;
import com.tkjt.pms.calc.formula.common.FormulaConsts;
import com.tkjt.pms.calc.formula.common.FormulaRule;
import com.tkjt.pms.calc.formula.common.FormulaRules;
import com.tkjt.pms.calc.formula.common.FormulaValue;
import com.tkjt.pms.calc.formula.utils.FormulaParamHelper;
import com.tkjt.pms.calc.utils.EsCalcUtils;
import com.tkjt.pms.common.IndexConsts;
import com.tkjt.pms.common.IndexParam;
import com.tkjt.pms.common.entity.TkDepartBasic;
import com.tkjt.pms.common.entity.TkDepartGroup;
import com.tkjt.pms.common.entity.TkIndexInfoBasic;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueAttrs;
import com.tkjt.pms.common.entity.TkIndexValueBasicRule;
import com.tkjt.pms.common.utils.DoubleUtil;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.modules.common.mapper.TkDepartBasicMapper;
import com.tkjt.pms.modules.common.mapper.TkDepartGroupMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexInfoBasicMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAttrsMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueBasicRuleMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.repos.common.BasicCalcParallel;
import com.tkjt.pms.modules.repos.mapper.BasicDataCalcMapper;
import com.tkjt.pms.modules.repos.service.BasicDataCalcService;
import com.tkjt.pms.persist.mapper.PmsMapper;
import com.tkjt.pms.server.common.ServerParam;
import com.tkjt.pms.utils.EsExceptionUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BasicDataCalcServiceImpl implements BasicDataCalcService {
    @Autowired
    private TkDepartBasicMapper tkDepartBasicMapper;
    @Autowired
    private TkIndexValueBasicRuleMapper tkIndexValueBasicRuleMapper;
    @Autowired
    private TkIndexInfoBasicMapper tkIndexInfoBasicMapper;
    @Autowired
    private BasicDataCalcMapper basicDataCalcMapper;
    @Autowired
    TkDepartGroupMapper tkDepartGroupMapper;
    @Autowired
    private CalcTraceUpdater calcTraceUpdater;
    @Autowired
    private CalcDataUpdater calcDataUpdater;
    @Autowired
    private TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    private TkIndexValueAttrsMapper tkIndexValueAttrsMapper;
    @Autowired
    private PmsMapper pmsMapper;
    @Autowired
    private Map<String, FormulaEngine> formulaEngines = new HashMap<>();
    private ExecutorService executorService = Executors.newFixedThreadPool(5);
    @Override
    public void basicDataCalc(String indexGroup,String dataType,String indexValueCode,String indexDepart) {
        String indexYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String calcCode = EsCalcUtils.getCBatchCode(indexYearMonth);
        Date calcTimeStart = new Date();
        Date calcTimeDepen = new Date();
        //获取需要计算的部门集合
        List<BasicCalcParallel> IndexCalcParallels = loaderDepartInfoData(indexYearMonth,dataType,indexGroup,indexDepart,indexValueCode);
        for(BasicCalcParallel basicCalcParallel : IndexCalcParallels){
            IndexParam params = new ServerParam();
            params.addParam(IndexConsts.PARAM_INDEX_YEAR_MONTH, indexYearMonth);
            params.addParam(IndexConsts.PARAM_INDEX_CALC_SERIAL, basicCalcParallel);
            // 分析辅助信息
            params.addParam(IndexCalcConsts.PARAM_CALC_CBATCH_CODE, calcCode);
            params.addParam(IndexCalcConsts.PARAM_CALC_TIME_START, calcTimeStart);
            params.addParam(IndexCalcConsts.PARAM_CALC_TIME_DEPEN, calcTimeDepen);
            String pgroupNo = EsCalcUtils.getPGroupCode(indexYearMonth);
            params.addParam(IndexCalcConsts.PARAM_CALC_PGROUP_CODE, pgroupNo);
            Runnable runnable = new Runnable(){

                @Override
                public void run() {
                    preCalculate(params,basicCalcParallel);
                }
            };
            executorService.submit(runnable);
        }

    }

    public List<BasicCalcParallel> loaderDepartInfoData(String indexYearMonth,String dataType,String indexGroup,String indexDepart,String singleIndexValueCode){
        List<BasicCalcParallel> basicCalcParallels = new ArrayList<>();
        List<TkDepartBasic> basicDepartList = new ArrayList<>();
        QueryWrapper<TkDepartBasic> departBasicWrapper = new QueryWrapper<>();
        departBasicWrapper.lambda().orderByAsc(TkDepartBasic::getSort);
        departBasicWrapper.lambda().eq(TkDepartBasic::getIndexGroup,indexGroup);
        if(StringUtils.isNotEmpty(indexDepart)){
            departBasicWrapper.lambda().eq(TkDepartBasic::getIndexDepart,indexDepart);
        }
        basicDepartList = tkDepartBasicMapper.selectList(departBasicWrapper);
        //获取所有需要计算基础数据的数据项code集合
        Map<String,Object> infoQuery = new HashMap<>();
        infoQuery.put("orgGroup",indexGroup);
        List<String> indexAttrs = new ArrayList<>();
        /************************************************
         * KPI目标值数据不需要从系统中对接数据
         */
        if("budget".equals(dataType)) {
            indexAttrs.add(InfoAttrUtil.MU_BIAO_ZHI);
        }
        indexAttrs.add(InfoAttrUtil.SHI_JI_ZHI);
        infoQuery.put("indexAttrs",indexAttrs);
        if(StringUtils.isNotEmpty(singleIndexValueCode)){
            infoQuery.put("indexValueCode",singleIndexValueCode);
        }
        List<String> indexValueCodes = basicDataCalcMapper.selectIndexValueCodesByParam(infoQuery);
        for(String indexValueCode : indexValueCodes){
            for(TkDepartBasic tkDepartBasic : basicDepartList){
                Set<String> indexDeparts = new HashSet<>();
                indexDeparts.add(tkDepartBasic.getIndexDepart());
                List<DepartOwner> departOwners = pmsMapper.getDepartOwnerByCodes(indexDeparts);
                for(DepartOwner departOwner : departOwners){
                    BasicCalcParallel basicCalcParallel = new BasicCalcParallel();
                    basicCalcParallel.setIndexDepart(tkDepartBasic.getIndexDepart());
                    basicCalcParallel.setEntCode(tkDepartBasic.getEntCode());
                    basicCalcParallel.setFormCode(tkDepartBasic.getFormCode());
                    basicCalcParallel.setIndexBiz(tkDepartBasic.getBizCode());
                    basicCalcParallel.setIndexScorer(departOwner.getIndexScorer());
                    basicCalcParallel.setIndexValueCode(indexValueCode);
                    basicCalcParallel.setIndexYearMonth(indexYearMonth);
                    basicCalcParallel.setDataType(dataType);
                    basicCalcParallels.add(basicCalcParallel);
                }
            }
        }
        return basicCalcParallels;
    }

    public void preCalculate(IndexParam params,BasicCalcParallel basicCalcParallel){
        /********************************************************************************************************
         * Step 1: 加载数据项计算对应的配置公式
         */
        FormulaRules formulaRules = null;
        String exception = null;
        IndexCalcParallel indexCalcParallel = new IndexCalcParallel();
        BeanUtils.copyProperties(basicCalcParallel,indexCalcParallel);
        try {
            formulaRules = loaderRules(basicCalcParallel);
            params.addParam(IndexCalcConsts.PARAM_CALC_CONFIG_RULES, formulaRules);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            exception = EsExceptionUtils.formatException(e);
        }
        if (null != exception) {
            calcTraceUpdater.updateException(params, indexCalcParallel, exception);
        }
        /********************************************************************************************************
         * Step 2: 处理需要计算的[DataColumn], 必须为List类型( 月份列、季度列、累计列)
         */
        List<String> calcWhichDatas = FormulaConsts.basicCalcWhichDatas;
        params.addParam(FormulaConsts.ITEM_CONFIG_WHICH_DATAS, calcWhichDatas);
        /********************************************************************************************************
         * Step 3: 按指定的顺序计算： 月份列、季度列、累计列
         */
        for (String whichData : calcWhichDatas) {
            FormulaRule formulaRule = formulaRules.searchRule(whichData, basicCalcParallel.getIndexValueCode(), basicCalcParallel.getIndexDepart());
            if (null == formulaRule || com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(formulaRule.getFormula())) {
                log.warn("数据项：{}、whichData：{}、部门：{}公式不存在", basicCalcParallel.getIndexValueCode(),whichData, basicCalcParallel.getIndexDepart());
                continue;
            }
            params = FormulaParamHelper.setFormulaCurrentWhichData(params, whichData);
            boolean whichDataRet = doCalculate(params,whichData,formulaRule,basicCalcParallel);
            if (whichDataRet) {
                calcTraceUpdater.updateCalcTraceSuccess(params, indexCalcParallel);
            } else {
                calcTraceUpdater.updateCalcTraceException(params, indexCalcParallel);
            }
            params.removeParam(whichData);
        }
    }

    public boolean doCalculate(IndexParam params,String whichData,FormulaRule formulaRule,BasicCalcParallel basicCalcParallel){
        /********************************************************************************************************
         * Step 1: 加载公式计算所需要的数据
         */
        FormulaValue formulaValue = new FormulaValue();
        try {
            params = FormulaParamHelper.addFormulaRule(params, whichData, formulaRule);
            Set<String> ruleVariables = new HashSet<>();
            for (String oneVarKey : FormulaConsts.formulaVariableKeys) {
                Set<String> oneResults = EsCalcUtils.searchOneVariableKey(oneVarKey, formulaRule.getFormula());
                if (!oneResults.isEmpty()) {
                    ruleVariables.addAll(oneResults);
                }
            }
            for (String variable : ruleVariables) {
                List<Double> variablesData = loadVariableData(variable,basicCalcParallel);
                formulaValue.addValue(variable, variablesData);
            }

            params = FormulaParamHelper.addFormulaValue(params, whichData, formulaValue);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String exception = EsExceptionUtils.formatException(e);
            params = FormulaParamHelper.addFormulaException(params, whichData, exception);
            return false;
        }

        /********************************************************************************************************
         * Step 3: 执行计算，返回计算结果
         */
        EngineResult engineResult = null;
        try {
            String engineCode = FormulaConsts.TYPE_ENGINE_EXCEL;
            log.info("engineCode = {}", engineCode);
            FormulaEngine formulaEngine = formulaEngines.get(engineCode);
            engineResult = formulaEngine.doExecutor(params, formulaRule, formulaValue);
            params = FormulaParamHelper.addFormulaResult(params, whichData, engineResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String exception = EsExceptionUtils.formatException(e);
            params = FormulaParamHelper.addFormulaException(params, whichData, exception);
            return false;
        }
        /********************************************************************************************************
         * Step 4: 保存当前计算结果
         */
        try {
            Map<String, Double> resultItems = engineResult.getCalcColumnValues();
            IndexCalcParallel indexCalcParallel = new IndexCalcParallel();
            BeanUtils.copyProperties(basicCalcParallel,indexCalcParallel);
            calcDataUpdater.doUpdater(params, indexCalcParallel, resultItems);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String exception = EsExceptionUtils.formatException(e);
            params = FormulaParamHelper.addFormulaException(params, whichData, exception);
            return false;
        }
        return true;
    }

    public FormulaRules loaderRules(BasicCalcParallel basicCalcParallel) {

        FormulaRules formulaRules = new FormulaRules();

        QueryWrapper<TkIndexValueBasicRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TkIndexValueBasicRule::getIndexValueCode, basicCalcParallel.getIndexValueCode());
        List<TkIndexValueBasicRule> tkIndexValueRules = tkIndexValueBasicRuleMapper.selectList(queryWrapper);
        for (TkIndexValueBasicRule rule : tkIndexValueRules) {
            String ruleIndexValueCode = rule.getIndexValueCode();
            String ruleWhichData = rule.getWhichData();
            String ruleIndexDepart = rule.getIndexDepart();
            if(StringUtils.isNotEmpty(ruleIndexDepart) && ruleIndexDepart.startsWith("ATRD")){
                QueryWrapper<TkDepartGroup> fepartGroupQueryWrapper = new QueryWrapper<>();
                fepartGroupQueryWrapper.lambda().eq(TkDepartGroup::getDepartBiz,ruleIndexDepart);
                List<TkDepartGroup> departGroups = tkDepartGroupMapper.selectList(fepartGroupQueryWrapper);
                for(TkDepartGroup tkDepartGroup : departGroups){
                    String ruleKey = EsCalcUtils.buildRuleKey(ruleIndexValueCode, ruleWhichData, tkDepartGroup.getIndexDepart());
                    FormulaRule formulaRule = new FormulaRule();
                    formulaRule.setFormula(EsCalcUtils.formatValueRule(rule.getIndexValueRule()));
                    formulaRule.setFormulaCN(EsCalcUtils.formatValueRule(rule.getIndexValueRuleCn()));
                    formulaRules.addFormula(ruleKey, formulaRule);
                }
            }else{
                String ruleKey = EsCalcUtils.buildRuleKey(ruleIndexValueCode, ruleWhichData, ruleIndexDepart);
                FormulaRule formulaRule = new FormulaRule();
                formulaRule.setFormula(EsCalcUtils.formatValueRule(rule.getIndexValueRule()));
                formulaRule.setFormulaCN(EsCalcUtils.formatValueRule(rule.getIndexValueRuleCn()));
                formulaRules.addFormula(ruleKey, formulaRule);
            }

        }
        return formulaRules;
    }

    /****************************************************
     * *@variable格式：
     1）Mi 之类 【没有@数据项，没有@部门，没有@ATR(其实也是部门类别)】
     2）Mi@数据项 之类【只有@数据项】
     3）Mi@部门  之类 【只有@部门】
     4）Mi@数据项@部门 之类
     5）Mi@ATR  之类 【只有@ATR】
     6）Mi@数据项@ATR  之类
     */
    private List<Double> loadVariableData(String variable, BasicCalcParallel basicCalcParallel) {

        List<Double> results = new ArrayList<Double>();
        TkIndexInfoBasic tkIndexInfoBasic = null;
        String replaceIndexValueCode = basicCalcParallel.getIndexValueCode();
        String calcIndexValueCode = basicCalcParallel.getIndexValueCode();
        String calcIndexDepart = basicCalcParallel.getIndexDepart();
        String indexScorner = basicCalcParallel.getIndexScorer();
        String indexYearMonth = basicCalcParallel.getIndexYearMonth();
        String entCode = basicCalcParallel.getEntCode();
        String formCode = basicCalcParallel.getFormCode();
        String indexBiz = basicCalcParallel.getIndexBiz();
        String indexYear = basicCalcParallel.getIndexYearMonth().substring(0,4);
        String[] tempArray = variable.split("@");
        for(int i = 0 ; i < tempArray.length ; i++ ){
            if(tempArray[i].startsWith("KGI") || tempArray[i].startsWith("KCI")){
                calcIndexValueCode = tempArray[i];
            }
            if (tempArray[i].startsWith("ATR")) {
                if("ATRT1000".equals(indexBiz)){
                    //筹开独立
                    if(tempArray[i].equals("ATRD1001")){
                        entCode = entCode.split("@")[1];
                        formCode = "B021";
                        indexBiz = tempArray[i];
                    }
                    //筹开护理
                    else if(tempArray[i].equals("ATRD1002")){
                        entCode = entCode.split("@")[1];
                        formCode = "B022";
                        indexBiz = tempArray[i];
                    }
                    //筹开医院
                    else{
                        entCode = entCode.split("@")[0];
                        formCode = null;
                        indexBiz = tempArray[i];
                    }
                }else{
                    TkDepartBasic tkDepartBasic = getSameGroupDepart(tempArray[i],calcIndexDepart);
                    entCode = tkDepartBasic.getEntCode();
                    formCode = tkDepartBasic.getFormCode();
                    indexBiz = tkDepartBasic.getBizCode();
                    indexScorner = "";
                }
            }
            if (tempArray[i].startsWith("A01A01")) {
                calcIndexDepart = tempArray[i];
                indexScorner = "";
            }
            if (tempArray[i].startsWith("JT")) {
                QueryWrapper<TkIndexInfoBasic> infoBasicWrapper = new QueryWrapper<>();
                infoBasicWrapper.lambda().eq(TkIndexInfoBasic::getReplaceCode,tempArray[i]);
                tkIndexInfoBasic = tkIndexInfoBasicMapper.selectOne(infoBasicWrapper);
                calcIndexValueCode = tkIndexInfoBasic.getKpiCode();
                replaceIndexValueCode = tempArray[i];
            }
        }
        if(replaceIndexValueCode.startsWith("JT")){
            //组装mtotal,m01,m02....m12,q01...q04格式数据
            String valueColumn = "budget";
            QueryWrapper<TkIndexValueAttrs> attrQueryWrapper = new QueryWrapper<>();
            attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,basicCalcParallel.getIndexValueCode());
            attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexAttr");
            TkIndexValueAttrs tkIndexValueAttrs = tkIndexValueAttrsMapper.selectOne(attrQueryWrapper);
            if(tkIndexValueAttrs.getAttrValue().equals(InfoAttrUtil.SHI_JI_ZHI)){
                valueColumn = "actual";
            }
            Map<String,Object> param = new HashMap<>();
            param.put("entCode",entCode);
            param.put("formCode",formCode);
            param.put("kpiCode",calcIndexValueCode);
            param.put("indexDepart",calcIndexDepart);
            List<String> levelcls = new ArrayList<>();
            if(indexBiz != null){
                if(indexBiz.equals("ATRD1001")){
                    levelcls.add("无");
                    levelcls.add("独立");
                }else if(indexBiz.equals("ATRD1002")){
                    levelcls.add("护理");
                }else{
                    levelcls.add("康复");
                }
            }

            //营业收入\净利润\EBITDA\所得税\委托管理费用\折旧与摊销-土地及房产\公允价值变动损益\月费收入\招聘培新&人事费用
            if("1".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay1(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(DoubleUtil.mul(Double.valueOf(valueStr),10000d));
                }
            }
            //折标收入
            else if("2".equals(tkIndexInfoBasic.getValueGetWay())){
                param.put("year",indexYearMonth.substring(0,4));
                param.put("levelcls",levelcls);
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonth",indexYear+month);
                    if(tkIndexValueAttrs.getAttrValue().equals(InfoAttrUtil.SHI_JI_ZHI)){
                        Double value = basicDataCalcMapper.getBasicValueForWay2_1(param);
                        results.add(value != null ? DoubleUtil.div(value,10000d) : 0.0);
                    }else{
                        Double value = basicDataCalcMapper.getBasicValueForWay2_2(param);
                        results.add(value != null ? value : 0.0);
                    }
                }
            }
            //净新增
            else if("3".equals(tkIndexInfoBasic.getValueGetWay())){
                param.put("levelcls",levelcls);
                for(int i = 0 ; i <= 16 ; i++){
                    String month = i < 10 ? "0"+i : String.valueOf(i);
                    param.put("yearMonth",indexYear+month);
                    Double value = basicDataCalcMapper.getBasicValueForWay3(param);
                    if("01".equals(month)){
                        param.put("yearMonth",Integer.valueOf(indexYear)-1+"12");
                    }else{
                        String lastMonth = (i-1) < 10 ? "0"+(i-1) : String.valueOf(i-1);
                        param.put("yearMonth",Integer.valueOf(indexYear)+lastMonth);
                    }
                    Double lastMonthValue = basicDataCalcMapper.getBasicValueForWay3(param);
                    results.add(DoubleUtil.sub(value != null ? value : 0.0,lastMonthValue != null ? lastMonthValue : 0.0));
                }
            }
            //KPI调整系数（运营收入）\KPI调整系数（折标收入）\KPI调整系数（成本费用）
            else if("4".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    Double value = basicDataCalcMapper.getBasicValueForWay4(param);
                    results.add(value);
                }
            }
            //餐饮收入
            else if("5".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay5(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(Double.valueOf(valueStr));
                }
            }
            //成本费用
            else if("6".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay6(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(DoubleUtil.div(Double.valueOf(valueStr),10000d));
                }
            }
            //保安保洁人力成本
            else if("7".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay7(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(DoubleUtil.div(Double.valueOf(valueStr),10000d));
                }
            }
            //维修维保和能源费用占营收比
            else if("8".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay8(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(Double.valueOf(valueStr));
                }
            }
            //人力成本占收入比
            else if("9".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonthDay",indexYear+month+"01");
                    Map<String,Object> value = basicDataCalcMapper.getBasicValueForWay9(param);
                    String valueStr = value != null && value.get(valueColumn) != null ? value.get(valueColumn).toString() : "0.0";
                    results.add(Double.valueOf(valueStr));
                }
            }
            //床位使用率
            else if("10".equals(tkIndexInfoBasic.getValueGetWay())){
                for(int i = 0 ; i <= 16 ; i++){
                    String month = "";
                    month = i < 10 ? "0"+i:String.valueOf(i);
                    param.put("yearMonth",indexYear+month);
                    Double value = basicDataCalcMapper.getBasicValueForWay10(param);
                    results.add(value == null ? 0.0 : value);
                }
            }//净预订
            else if("11".equals(tkIndexInfoBasic.getValueGetWay())){
                List<String> levelcls11 = new ArrayList<>();
                levelcls11.add("独立");
                levelcls11.add("护理");
                param.put("levelcls",levelcls11);
                for(int i = 0 ; i <= 16 ; i++){
                    String month = i < 10 ? "0"+i : String.valueOf(i);
                    param.put("yearMonth",indexYear+month);
                    Double value = basicDataCalcMapper.getBasicValueForWay11(param);

                    results.add(value != null ? value : 0.0);
                }
            }
        }else{
            QueryWrapper<TkIndexValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TkIndexValue::getIndexValueCode, calcIndexValueCode);
            queryWrapper.lambda().eq(TkIndexValue::getIndexDepart, calcIndexDepart);
            if(StringUtils.isNotEmpty(indexScorner)){
                queryWrapper.lambda().eq(TkIndexValue::getIndexScorer,indexScorner);
            }
            queryWrapper.lambda().eq(TkIndexValue::getDataType, basicCalcParallel.getDataType());
            List<TkIndexValue> tkIndexValues = tkIndexValueMapper.selectList(queryWrapper);
            TkIndexValue tkIndexValue = new TkIndexValue();
            if (tkIndexValues.size()> 0) {
                tkIndexValue = tkIndexValues.get(0);
            }
            Map<String, Object> beanValues = EsJsonUtils.bean2Map(tkIndexValue);
            for (String key : FormulaConsts.orderKeys) {
                results.add((Double) beanValues.get(key));
            }
        }
        return results;

    }
    public TkDepartBasic getSameGroupDepart(String departBiz,String calcIndexDepart){
        QueryWrapper<TkDepartGroup> departGroupQuery = new QueryWrapper<>();
        departGroupQuery.lambda().eq(TkDepartGroup::getIndexDepart,calcIndexDepart);
        TkDepartGroup groupDepart = tkDepartGroupMapper.selectOne(departGroupQuery);
        departGroupQuery = new QueryWrapper<>();
        departGroupQuery.lambda().eq(TkDepartGroup::getDepartGroup,groupDepart.getDepartGroup());
        departGroupQuery.lambda().eq(TkDepartGroup::getDepartBiz,departBiz);
        groupDepart = tkDepartGroupMapper.selectOne(departGroupQuery);
        calcIndexDepart = groupDepart.getIndexDepart();
        QueryWrapper<TkDepartBasic> departBasicWrapper = new QueryWrapper<>();
        departBasicWrapper.lambda().eq(TkDepartBasic::getIndexDepart,calcIndexDepart);
        TkDepartBasic tkDepartBasic = tkDepartBasicMapper.selectOne(departBasicWrapper);
        return tkDepartBasic;
    }
}
